﻿//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "stdafx.h"
#include "Form_Learning.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Chart"
#pragma link "TeEngine"
#pragma link "TeeProcs"
#pragma link "Series"
#pragma resource "*.dfm"

TFormLearning *FormLearning;
//---------------------------------------------------------------------------
__fastcall TFormLearning::TFormLearning(TComponent* Owner)
	: TForm(Owner)
{
//	TChartSeries* a;
//	FormLearning->LearningChart->AddSeries(a);
	//paint_Grid();
	paint_Bar(true);

	FormLearning->GASeries->GetVertAxis->Title->Caption = "Fitness";
	FormLearning->GASeries->GetHorizAxis->Title->Caption = "Epoch";
}
//---------------------------------------------------------------------------
void __fastcall TFormLearning::StartBtnClick(TObject *Sender)
{
	//Fitness紀錄先歸0
	for(int Epoch=0; Epoch<MaxEpoch; Epoch++)
	{
		GA.Swarm.FitnessRecord[Epoch] = 0;
		PSO.Swarm.FitnessRecord[Epoch] = 0;
		GAPSO.Swarm.FitnessRecord[Epoch] = 0;
		EPSO.Swarm.FitnessRecord[Epoch] = 0;
		APEGA.Swarm.FitnessRecord[Epoch] = 0;
    }

	FormLearning->GASeries->Clear();
	FormLearning->PSOSeries->Clear();
	FormLearning->GAPSOSeries->Clear();
	FormLearning->EPSOSeries->Clear();
	FormLearning->APEGASeries->Clear();

	for(int i=0; i<MeanTime; i++)
	{
		paint_Bar(true);
		GA.Process(true, Chord_Box_1->ItemIndex+1, Chord_Box_2->ItemIndex+1, Chord_Box_3->ItemIndex+1 , Chord_Box_4->ItemIndex+1);
		PSO.Process(true, Chord_Box_1->ItemIndex+1, Chord_Box_2->ItemIndex+1, Chord_Box_3->ItemIndex+1 , Chord_Box_4->ItemIndex+1);
		EPSO.Process(true, Chord_Box_1->ItemIndex+1, Chord_Box_2->ItemIndex+1, Chord_Box_3->ItemIndex+1 , Chord_Box_4->ItemIndex+1);
		GAPSO.Process(true, Chord_Box_1->ItemIndex+1, Chord_Box_2->ItemIndex+1, Chord_Box_3->ItemIndex+1 , Chord_Box_4->ItemIndex+1);
		APEGA.Process(true, Chord_Box_1->ItemIndex+1, Chord_Box_2->ItemIndex+1, Chord_Box_3->ItemIndex+1 , Chord_Box_4->ItemIndex+1);
	}

    //平均
	for(int Epoch=0; Epoch<MaxEpoch; Epoch++)
	{
		GA.Swarm.FitnessRecord[Epoch] /= (float)MeanTime;
		PSO.Swarm.FitnessRecord[Epoch] /= (float)MeanTime;
		GAPSO.Swarm.FitnessRecord[Epoch] /= (float)MeanTime;
		EPSO.Swarm.FitnessRecord[Epoch] /= (float)MeanTime;
		APEGA.Swarm.FitnessRecord[Epoch] /= (float)MeanTime;

		FormLearning->GASeries->AddXY(Epoch, GA.Swarm.FitnessRecord[Epoch]);
		FormLearning->PSOSeries->AddXY(Epoch, PSO.Swarm.FitnessRecord[Epoch]);
		FormLearning->GAPSOSeries->AddXY(Epoch, GAPSO.Swarm.FitnessRecord[Epoch]);
		FormLearning->EPSOSeries->AddXY(Epoch, EPSO.Swarm.FitnessRecord[Epoch]);
		FormLearning->APEGASeries->AddXY(Epoch, APEGA.Swarm.FitnessRecord[Epoch]);

		FormLearning->GALabel->Caption = "GA Fitness: " + IntToStr(GA.Swarm.FitnessRecord[Epoch]);
		FormLearning->PSOLabel->Caption = "PSO Fitness: " + IntToStr(PSO.Swarm.FitnessRecord[Epoch]);
		FormLearning->GAPSOLabel->Caption = "GAPSO Fitness: " + IntToStr(GAPSO.Swarm.FitnessRecord[Epoch]);
		FormLearning->EPSOLabel->Caption = "EPSO Fitness: " + IntToStr(EPSO.Swarm.FitnessRecord[Epoch]);
		FormLearning->APEGALabel->Caption = "APEGA Fitness: " + IntToStr(APEGA.Swarm.FitnessRecord[Epoch]);
	}
}
//---------------------------------------------------------------------------





